// pages/dynamic/dynamic.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dynamic: {
      type: Array,
      value: []
    },
    duration: {
      type: Number | String,
      value: '5000'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    content: '',
    index: 0,
    timer: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
  },
  lifetimes: {
    attached: function () {
      var duration = this.data.duration * 1
      this.setData({
        content: this.data.dynamic[0].content,
        index: this.data.index + 1,
        duration: (this.data.duration / 1000).toFixed(1)
      })
    
      this.data.timer = setInterval(() => {
        if (this.data.index == this.data.dynamic.length) {
          this.setData({
            index: 0
          })
        }
        this.setData({
          content: this.data.dynamic[this.data.index].content,
          index: this.data.index + 1,
        })
      }, duration);
    },
    detached: function () {
      delete this.data.timer
    }
  }
})
